package com.tianci.appstore.ui.list.locallist;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.controller.AppStoreMainPageController;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.data.SkyListAppData.ListType;
import com.tianci.appstore.ui.list.manage.ManageBaseListView;

import java.util.List;

public class LocalListView extends ManageBaseListView implements LocalListDataListener {
    public LocalListView(Context context) {
        super(context, ListType.LOCAL_APP_LIST);
        // TODO Auto-generated constructor stub
        AppStoreMainPageController.getInstance(context).setLocalListDataListener(this);
        AppStoreMainPageController.getInstance(context).showMoreAppData();
        setLoadingVisible(true);
    }

    @Override
    public void destroyController() {
        // TODO Auto-generated method stub
        super.destroyController();
    }

    @Override
    public void onBlockClicked(View v, SkyListAppData data, int index) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onLocalListLoaded(List<SkyListAppData> dataList) {
        // TODO Auto-generated method stub
        LogUtils.d("locallist", "local list loaded, size = " + dataList.size());
        Message msg = mainHandler.obtainMessage(UPDATE_LIST);
        msg.obj = dataList;
        mainHandler.sendMessage(msg);
    }

    private void refreshList(List<SkyListAppData> dataList) {
        setLoadingVisible(false);
        mController.setDataList(dataList);
        resetFocus();
    }

    protected Handler mainHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            switch (msg.what) {
                case UPDATE_LIST:
                    refreshList((List<SkyListAppData>) msg.obj);
                    break;
            }
            super.handleMessage(msg);
        }
    };
}
